# -*- coding:utf-8 -*-
from common_module.pub_utils import is_other


__author__ = 'root'
from django.core.management.base import BaseCommand
import os
from kao_question.models import *
from kao_media.models import *
from common_module.path_handle import base_path, source_path_handle, source_file_copy
import logging
log = logging.getLogger('django_crontab.crontab')
kaoala_jieba_dict=os.path.join(base_path,'kaoala_dict.txt')    #kaoala分词词汇文件路径

from common_module.source_handles.kaoala_source_handle import dataframe_init,proportion_material_table,material_solidify_table
from common_module.source_handles.kaoala_source_datafeici import source_seach_char

def fenci_question():
    question_qbody_dict={}
    question_qhins_dict={}
    question_pic_dcit={}

    answer_answerText_dcit={}
    answer_corrent_dict={}

    video_desc_dict={}

    video_dict={}

    word_question_qbody_dict={}
    word_question_qhins_dict={}
    word_answer_answerText_dcit={}
    word_answer_corrent_dict={}
    word_video_desc_dict={}





    log.info(u'start 提取题目数据')
    question_list=kao_question.objects.all()
    for sub_question in question_list:
        qbody=sub_question.qbody
        if qbody!=None and qbody!='':
            question_qbody_dict[int(sub_question.id)]=qbody
        qhins=sub_question.qhins
        if qhins!=None and qhins!='':
            question_qhins_dict[int(sub_question.id)]=qhins

        if sub_question.pic!=None and sub_question.pic!='':
            question_pic_dcit[int(sub_question.id)]=sub_question.pic
    log.info(u'end 提取题目数据')

    log.info(u'start 提取答案数据')
    answer_list=kao_answer.objects.all()
    for sub_answer in answer_list:
        if sub_answer.answerText!=None and sub_answer.answerText!='':
            answer_answerText_dcit[int(sub_answer.id)]=sub_answer.answerText
        if sub_answer.corrent_answer!=None and sub_answer.corrent_answer!='':
            answer_corrent_dict[int(sub_answer.id)]=sub_answer.corrent_answer
    log.info(u'end 提取答案数据')

    log.info(u'start 提取视频描述数据')
    desc_list=subject_desc.objects.all()
    for sub_desc in desc_list:
        if sub_desc.video_conclusion!=None and sub_desc.video_conclusion!='':
            video_desc_dict[int(sub_desc.subject_id)]=sub_desc.video_conclusion

    log.info(u'end 提取视频描述数据')

    log.info(u'start 提取视频地址数据')
    video_list=material_manager.objects.filter(mtype=1,purpose=0)
    for sub_video in video_list:
        video_dict[int(sub_video.subject.all()[0].id)]=sub_video.material_url
    log.info(u'end 提取视频地址数据')


    log.info(u'start 进行分词')
    all_text_list=question_qbody_dict.values()+question_qhins_dict.values()+answer_answerText_dcit.values()+answer_corrent_dict.values()+video_desc_dict.values()
    all_text=u''.join(all_text_list)
    items=source_seach_char(seach_text=all_text)
    all_word_list=[]
    if items['code']==0:
        all_word_list=list(set(items['data']))
    log.info(u'end 进行分词')




    log.info(u'start 对题目qbody进行分词')
    for key,value in question_qbody_dict.items():
        new_str=u''
        for sub_key in value:
            if is_other(sub_key)==True:  #判断单个字符是否为符号
                continue
            else:
                new_str=new_str+sub_key
        one_word_list=[]
        for sub_word in all_word_list:
            if sub_word in new_str:
                one_word_list.append(sub_word)
        word_question_qbody_dict[key]=one_word_list
    log.info(u'end 对题目qbody进行分词')

    log.info(u'start 对题目qhins进行分词')
    for key,value in question_qhins_dict.items():
        new_str=u''
        for sub_key in value:
            if is_other(sub_key)==True:  #判断单个字符是否为符号
                continue
            else:
                new_str=new_str+sub_key
        one_word_list=[]
        for sub_word in all_word_list:
            if sub_word in new_str:
                one_word_list.append(sub_word)
        word_question_qhins_dict[key]=one_word_list
    log.info(u'end 对题目qhins进行分词')


    log.info(u'start 对答案answerText进行分词')
    for key,value in answer_answerText_dcit.items():
        new_str=u''
        for sub_key in value:
            if is_other(sub_key)==True:  #判断单个字符是否为符号
                continue
            else:
                new_str=new_str+sub_key
        one_word_list=[]
        for sub_word in all_word_list:
            if sub_word in new_str:
                one_word_list.append(sub_word)
        word_answer_answerText_dcit[key]=one_word_list
    log.info(u'end 对答案answerText进行分词')


    log.info(u'start 对答案corrent进行分词')
    for key,value in answer_corrent_dict.items():
        new_str=u''
        for sub_key in value:
            if is_other(sub_key)==True:  #判断单个字符是否为符号
                continue
            else:
                new_str=new_str+sub_key
        one_word_list=[]
        for sub_word in all_word_list:
            if sub_word in new_str:
                one_word_list.append(sub_word)
        word_answer_corrent_dict[key]=one_word_list
    log.info(u'end 对答案corrent进行分词')


    log.info(u'start 对视频描述进行分词')
    for key,value in video_desc_dict.items():
        new_str=u''
        for sub_key in value:
            if is_other(sub_key)==True:  #判断单个字符是否为符号
                continue
            else:
                new_str=new_str+sub_key
        one_word_list=[]
        for sub_word in all_word_list:
            if sub_word in new_str:
                one_word_list.append(sub_word)
        word_video_desc_dict[key]=one_word_list
    log.info(u'end 对视频描述进行分词')



    log.info(u'start 加载所有的表的dataframe')
    dataframe_init_obj=dataframe_init(word_dict_path=kaoala_jieba_dict)
    dataframe_init_obj.load_ram()
    log.info(u'end 加载所有的表的dataframe')


    log.info(u'start 对权重与素材映射进行处理')
    proportion_material_table_obj=proportion_material_table(dataframe_init_obj.word_dataframe,dataframe_init_obj.word_material_proportion_dataframe,dataframe_init_obj.proportion_material_6_dataframe)
    material_solidify_table_obj=material_solidify_table(dataframe_init_obj.material_solidify_dataframe)

    dataframe_init_obj.release_ram()


    for key,value in word_question_qbody_dict.items():
        proportion_material_table_obj.write_proportion_material(word_list=value,p_category=1,material_id=key)

    for key,value in word_question_qhins_dict.items():
        proportion_material_table_obj.write_proportion_material(word_list=value,p_category=3,material_id=key)

    for key,value in question_pic_dcit.items():
        pic_word_list=word_question_qbody_dict.get(key,[])+word_question_qhins_dict.get(key,[])
        pic_word_list=list(set(pic_word_list))
        proportion_material_table_obj.write_proportion_material(word_list=pic_word_list,p_category=2,material_id=key)

    for key,value in word_answer_answerText_dcit.items():
        proportion_material_table_obj.write_proportion_material(word_list=value,p_category=4,material_id=key)

    for key,value in word_answer_corrent_dict.items():
        proportion_material_table_obj.write_proportion_material(word_list=value,p_category=4,material_id=key)

    for key,value in word_video_desc_dict.items():
        proportion_material_table_obj.write_proportion_material(word_list=value,p_category=6,material_id=key)

    proportion_material_table_obj.solidify()
    proportion_material_table_obj.release_ram()
    log.info(u'end 对权重与素材映射进行处理')

    log.info(u'start 对素材表进行处理')

    for key,value in question_qbody_dict.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=1)

    for key,value in question_qhins_dict.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=3)

    for key,value in question_pic_dcit.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=2,pic_meida_path=value)

    for key,value in answer_answerText_dcit.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=4)

    for key,value in answer_corrent_dict.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=4)

    for key,value in video_desc_dict.items():
        material_solidify_table_obj.write_material_solidify(material_id=key,p_category=6,pic_meida_path=video_dict.get(key,''))

    material_solidify_table_obj.solidify()
    log.info(u'end 对素材表进行处理')










    # for key,value in question_pic_dcit.items():
    #     handle_material_obj.write_proportion_material(text=value,p_category=1,material_id=key)
    #     solidify_obj.write_material_solidify(material_id=key,p_category=1)
    #
    # for key,value in question_qhins_dict.items():
    #     handle_material_obj.write_proportion_material(text=value,p_category=3,material_id=key)
    #     solidify_obj.write_material_solidify(material_id=key,p_category=3)
    #
    # for key,value in question_pic_dcit.items():
    #     desc=question_qbody_dict.get(key,'')+question_qhins_dict.get(key,'')
    #     if desc!='':
    #         handle_material_obj.write_proportion_material(text=desc,p_category=2,material_id=key)
    #         solidify_obj.write_material_solidify(material_id=key,p_category=2,pic_meida_path=value)
    #
    # for key,value in answer_answerText_dcit.items():
    #     handle_material_obj.write_proportion_material(text=value,p_category=4,material_id=key)
    #     solidify_obj.write_material_solidify(material_id=key,p_category=4)
    #
    # for key,value in answer_corrent_dict.items():
    #     handle_material_obj.write_proportion_material(text=value,p_category=4,material_id=key)
    #     solidify_obj.write_material_solidify(material_id=key,p_category=4)
    #
    #
    # for key,value in video_desc_dict.items():
    #     handle_material_obj.write_proportion_material(text=value,p_category=6,material_id=key)
    #
    #     material_list=material_manager.objects.filter(subject__id=key,mtype=1,purpose=0)
    #     if material_list.exists()==True:
    #         solidify_obj.write_material_solidify(material_id=key,p_category=6,pic_meida_path=material_list[0].material_url)















class Command(BaseCommand):
    def handle(self, *args, **options):
        log.info(u'start 数据更新')

        log.info(u'start 进行文件夹处理')
        source_path_handle()
        log.info(u'end 文件夹处理')

        fenci_question()

        # log.info(u'start 分词词汇固化')
        # word_dict_obj=word_dict(word_dict_path=kaoala_jieba_dict)
        # word_dict_obj.writ_word_dict()
        # log.info(u'end 分词词汇固化')

        # log.info(u'start 固化分词与分词之前比重表')
        # word_word_proportion_obj=word_word_proportion()
        # word_word_proportion_obj.wird_proportion()
        # log.info(u'end 固化分词与分词之前比重表')

        # log.info(u'start 处理素材与分词进行对应')
        # fenci_question()
        # log.info(u'end 处理素材与分词进行对应')

        log.info(u'start 把处理好的源数据文件复制到前台文件夹中')
        source_file_copy()
        log.info(u'end 复制到前台文件夹中')

        log.info(u'end 数据更新')
